package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.ReportAdapter;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.bean.ReportBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: ReportActivity
 * @Description: 举报activity
 * @Author: ljp
 * @CreateDate: 2018/10/15 14:10
 * @Version: 1.0
 */
public class ReportActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.rela_1)
    RelativeLayout rela1;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_repost_know)
    TextView tvRepostKnow;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    private String[] mtitles;
    private List<ReportBean> reportBeans;
    private ReportAdapter reportAdapter;
    private String otherId;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String token;
    private String id;
    private String deviceInfo;
    private String report;
    private String TAG = "ReportActivity";
    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_report;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        id = mySeleInfo.getId();
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();

    }

    private void initView() {
        otherId = getIntent().getStringExtra("otherId");
        reportBeans = new ArrayList<>();
        mtitles = getResources().getStringArray(R.array.report_list);
        for (int i = 0; i < mtitles.length; i++) {
            reportBeans.add(new ReportBean(mtitles[i], false));
        }
        reportAdapter = new ReportAdapter(activity, reportBeans, R.layout.item_report);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(reportAdapter);
        reportAdapter.setSetCheck(new ReportAdapter.SetCheck() {
            @Override
            public void setChecked(int position, View view) {
                for (int i = 0; i < mtitles.length; i++) {
                    reportBeans.get(i).setChecked(false);
                }
                //然后设置点击的那个按钮设置状态为选中
                reportBeans.get(position).setChecked(true);    //这样所有的条目中只有一个被选中！
                reportAdapter.notifyDataSetChanged();//刷新适配器
                report = reportBeans.get(position).getTitle();
            }
        });
    }

    @OnClick({R.id.linear_back, R.id.tv_repost_know,R.id.tv_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_repost_know:
                startActivity(new Intent(activity, ReportKnowActivity.class));
                break;
            case R.id.tv_sure:
                if(!TextUtils.isEmpty(report)) {
                    setHttp();
                }else {
                    toast_MakeText("请选择举报原因");
                }
                break;
        }
    }
    /**
    * @method
    * @description 举报
    * @date: 2018/11/6 19:56
    * @author: ljp
    * @return
    */
    private void setHttp() {
        MeInfoService.reportAndComplain(mySeleInfo.getId(),token,deviceInfo,report,otherId,"1").subscribe(result -> {
            if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                Intent intent = new Intent(activity,AllSuccessActivity.class);
                intent.putExtra("flag","1");
                startActivity(intent);
                finish();
            }else if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(result.getMsg());
            }
            Log.i(TAG, "setHttp: ---------"+result.toString());
        },throwable -> {
            Log.i(TAG, "setHttp: ----------"+throwable.toString());
        });

    }


}
